package cn.itcast.controller;

import cn.itcast.domain.Cook;
import cn.itcast.domain.Customer;
import cn.itcast.domain.ResultInfo;
import cn.itcast.domain.User;
import cn.itcast.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService service;

    @RequestMapping("/register.do")
    @ResponseBody
    public ResultInfo register(User user, HttpServletRequest request, HttpServletResponse response) {
        ResultInfo info = new ResultInfo();
        /* ModelAndView mv = new ModelAndView();*/
        //验证校验
        String check = request.getParameter("check");
        //从session中获取验证码
        HttpSession session = request.getSession();
        String randomcodekey = (String) session.getAttribute("randomcode_key");
        if (randomcodekey == null || !randomcodekey.equalsIgnoreCase(check)) {
            //验证码错误
            /*mv.addObject("obb", "错误");
            mv.setViewName("demo");*/
            info.setFlag(false);
            info.setErrorMsg("验证码错误");
            return info;
        }


        Boolean flag = service.register(user);


        if (flag) {
            info.setFlag(true);
        } else {
            info.setFlag(false);
            info.setErrorMsg("用户不存在，注册失败");
        }
       /* ResultInfo info=new ResultInfo();
        if(flag){
            info.setFlag(true);
        }else{
            info.setFlag(false);
            info.setErrorMsg("注册失败");
        }

        */
        return info;
    }
//登录
    @RequestMapping("/login.do")
    @ResponseBody
    public Map UserLogin(HttpServletRequest request) {
        Map<String, Object> objmap = new HashMap<>();
        String result = null;
        String name = request.getParameter("username");
        String pwt = request.getParameter("password");
        String str=name.substring(0,1);
        if(str.equals("u")){
            Customer customer = service.Userlogin(name, pwt);
            if (customer == null || customer.equals("")) {
                result = "0";
            } else {
                result = "1";
                request.getSession().setAttribute("login_name",customer.getName());
                request.getSession().setAttribute("sign",str);
            }


        }else if(str.equals("c")){
            Cook cook = service.cookLogin(name, pwt);
            if (cook == null || cook.equals("")) {
                result = "0";
            } else {
                result = "1";
                request.getSession().setAttribute("login_name",cook.getName());
                request.getSession().setAttribute("sign",str);
            }
        }
        objmap.put("result", result);
        return objmap;
    }
    //获取登录信息
    @RequestMapping("/getLogin.do")
    @ResponseBody
    public Map getlogin(HttpServletRequest request){
        Map<String,Object> objMap=new HashMap<>();
        HttpSession session = request.getSession();
        String login_name = (String)request.getSession().getAttribute("login_name");
        String sign = (String) session.getAttribute("sign");
        if(sign!=null&&sign.equals("u")){
            if(login_name!=null&&!"".equals(login_name)){
                Customer user = service.fetchUserinfoByName(login_name);
                objMap.put("result",login_name);
                if(user!=null){
                    objMap.put("userinfo",user);
                    objMap.put("userinfoR",1);
                    objMap.put("sign",sign);
                }else {
                    objMap.put("userinfo",0);
                }
            }else{
                objMap.put("result",0);
            }
        }else if(sign!=null&&sign.equals("c")){
            if(login_name!=null&&!"".equals(login_name)){
                Cook cook = service.fetchCookinfoByName(login_name);
                objMap.put("result",login_name);
                if(cook!=null){
                    objMap.put("userinfo",cook);
                    objMap.put("userinfoR",1);
                    objMap.put("sign",sign);
                }else {
                    objMap.put("userinfo",0);
                }
            }else{
                objMap.put("result",0);
            }
        }else{
            objMap.put("result",0);
        }


        return objMap;
    }
    //登出
    @RequestMapping("/logout.do")
    @ResponseBody
    public Map logout(HttpServletRequest request){
        Map<String,Object> objMap=new HashMap<>();
        request.getSession().removeAttribute("login_name");
        objMap.put("result",1);
        return objMap;
    }

}


